<template>
	<right-layout :title="title" width="800px" :visible.sync="show" @closeLayout="closeLayout">
		<div class="layout-wrap">
			<el-form
				label-width="160px"
				label-position="left"
				label-suffix="："
				:model="ruleForm"
				:rules="rules"
				ref="form"
			>
				<el-form-item label="名称" prop="batchname">
					<el-input v-model="ruleForm.batchname" placeholder="请输入案例名称"></el-input>
				</el-form-item>
				<el-form-item label="参考模型Id" prop="modelid">
					<el-input v-model="ruleForm.modelid"></el-input>
				</el-form-item>
				<el-form-item label="需要结算的模块" prop="model">
					<el-checkbox-group v-model="ruleForm.model" @change='modelChange'>
						<el-checkbox label="1" disabled name="type">基本模块</el-checkbox>
						<el-checkbox label="2" name="type">早期模块</el-checkbox>
						<el-checkbox label="3" name="type">长期模块</el-checkbox>
						<el-checkbox label="3" name="type">食入模块</el-checkbox>
					</el-checkbox-group>
				</el-form-item>
			</el-form>
		</div>
		<div slot="foot">
			<div class="left">
				<el-button type="text" @click="closeLayout">关闭</el-button>
			</div>
			<el-button type="primary" :loading="loading" @click="submitFun(true)">保存</el-button>
			<el-button @click="resetForm">重置</el-button>
		</div>
	</right-layout>
</template>

<script>
import RightLayout from "@/components/common/comp/RightLayout"
import textLength from "@/components/textLength"
import selectPara from "@/components/selectPara"
import commCheckbox from "@/components/commCheckbox"
import { mapState } from "vuex"


export default {
	name: "GdBrandClassifyAdd",
	props: {},
	components: {
		textLength,
		selectPara,
		commCheckbox,
		RightLayout,
	},
	data() {
		var validate_descr = (rule, value, callback) => {
			if (this.ruleForm.descr_l > 256) {
				callback(new Error())
			} else {
				callback()
			}
		}
		return {
			config: {},
			title: "创建新案例",
			show: false,
			ruleForm: {
				id: "",
				batchname: "",
				modelid: 1174,
				isOpen: false,
				openDt: [],
				workDt: "",
				activityDt: "",
				svalue2: "",
				svalue: [],
				slabel2: "",
				slabel: [],
				rvalue: [],
				rlabel: [],
				rvalue2: "",
				rlabel2: "",
				sort: 0,
				descr: "",
				descr_l: 0,
				model:['1']
			},
			rules: {
				batchname: [
					{
						type: "string",
						message: "请输入正确的字符串",
					},
					{
						required: true,
						message: "名称必须填写",
					},
					{
						pattern: /^[\w\W]{0,20}$/,
						message: "长度限制0-20位",
					},
				],
				openDt: [
					{
						required: true,
						message: "请选择开始时间",
						trigger: "blur",
					},
				],
				sort: [
					{
						pattern: /^[\w\W]{0,4}$/,
						message: "长度限制0-4位",
					},
				],
				descr: [
					{
						validator: validate_descr,
						message: "长度限制256字",
						trigger: "blur",
					},
				],
			},
			loading: false,
		}
	},
	computed: {
		...mapState({
			token: (state) => state.token.token,
		}),
	},
	mounted() {},
	methods: {
		modelChange(){
			if(this.ruleForm.model.indexOf('3')!=-1){
				this.ruleForm.model.push('2')
			}
		},
		showLayout(data) {
			this.ruleForm.id = ""
			this.show = true
		},
		closeLayout() {
			this.show = false
			this.resetForm()
		},

		resetForm() {
			this.$refs.form.resetFields()
		},
		submitFun(flag) {
			this.$refs.form.validate((valid) => {
				if (valid) {
					let caidantype = '1'
					if(this.ruleForm.model.indexOf('2')!=-1){
						caidantype = '2'
					}
					if(this.ruleForm.model.indexOf('3')!=-1){
						caidantype = '3'
					}	
					let params = {
						batchname: this.ruleForm.batchname,
						modelid: this.ruleForm.modelid,
						caidantype: caidantype
					} 
					this.loading = true
					this.$appApi
						.BATCH_CREATE(params)
						.then((res) => {
							this.loading = false
							var str = res.data.substring(0, 1)
							if (str == "E") {
								let message = res.data.substring(6)
								this.$notify.error({
									title: "新增失败！",
									type: message,
								})
							} else if (str == "S") {
								this.$notify({
									message: "新增成功！",
									type: "success",
								})
								this.$emit("refresh")
								this.closeLayout()
							}
						})
						.catch((err) => {
							console.log(err)
						})
				}
			})
		},
	},
}
</script>